<?php

namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\Request;

class Con2 extends Login
{
    public function func1()
    {	
    	$list = Db::table('hc_juese')->field(['uid', 'juese'])->order(['uid' => 'ASC'])->select();
    	// var_dump($list);die;
        return view('admin@con2/func1', [
            'title' => '角色列表',
            'list' => $list
        ]);
    }

    public function addjuese()
    {
        return view('addjuese', [
            'title' => '添加角色',
           
        ]);
    }

    public function doaddjuese(Request $request)
    {   
        // var_dump($request);
      $p = $request->post();
         // 手动处理
        $data = [
            'juese' => $p['juese'],
            
        ];
          $result = Db::table('hc_juese')->data($data)->insert();

          if ($result > 0) {
            return $this->success('安排上了~', url('/admin/juese'));
        } else {
            return $this->error('还未安排上,请换个姿势');
        }
       // var_dump('这里已经到了添加准备SQL方法的一步了');
    }

    public function jj($uid)
    {   


      // var_dump($uid);
      $sql = "SELECT * FROM `hc_jiedian`";
        $role = Db::query($sql);
        // var_dump($role);
        $data = Db::table('hc_jj')->field('cid')->where('uid = '.$uid)->select();
        // var_dump($data);
        foreach ($role as $k => $v) {
           
          $role[$k]['check'] = null;
            foreach ($data as $u=>$c) {
                if ($v['cid'] == $c['cid']) {
                    $role[$k]['check'] = 'checked';

                }
            }
        }
        // var_dump($role);die;


        return view('addjj',[
            'role' => $role,
            'uid' =>$uid
            ]);
      // var_dump('这里已经到了添加准备SQL方法的一步了');
    }

    public function  doaddjj()
    { 

    
      $p = Request::instance()->post();
     
     
      
      $data['uid'] = $p['uid'];
   
         $data['cid'] = $p['rid'];
       Db::name('hc_jj')->where('uid',$data['uid'])->delete(); 

      foreach ($data['cid'] as $key => $v) {
         $list = [
                    'uid' => $data['uid'],
                    'cid' => $v
                ];
        
        // var_dump($data);die;
         $result = Db::name('hc_jj')->insert($list);


      }
      if ($result > 0 ){
            return $this->success('新增成功',url('/admin/juese'));
        }else{
            return $this->success('新增失败');
        }




      // var_dump('到做添加节点的控制器了');
    }
}
